import 'package:electronic_pet/pages/LoginView.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:hotkey_manager/hotkey_manager.dart';
import 'package:window_manager/window_manager.dart';

class Bootstrap extends StatefulWidget {
  const Bootstrap({super.key});

  @override
  State<Bootstrap> createState() => _Bootstrap();
}

class _Bootstrap extends State<Bootstrap> {

  @override
  void initState() {
    super.initState();
    Future.delayed(const Duration(milliseconds: 1)).then((_) => _init());
  }

  Future<void> _init() async {
    // Navigator.of(context).pushReplacement(
    //   // PageTransition 是转场动画
    //   PageTransition(
    //     type: PageTransitionType.fade,
    //     child: const DesktopPopupPage(),
    //   ),
    Navigator.of(context).pushReplacement(
        // PageTransition 是转场动画
        MaterialPageRoute(builder: (context) => const LoginView()));

    await hotKeyManager.unregisterAll();
    await hotKeyManager.register(
      HotKey(
        KeyCode.digit1,
        modifiers: [KeyModifier.alt],
        identifier: 'shortcut_show_or_hide',
      ),
      keyDownHandler: (_) {
        windowManager.show();
      },
    );
  }

  Widget _build(BuildContext context) {
    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: SystemUiOverlayStyle.dark,
      child: Scaffold(
        body: SizedBox(
          width: MediaQuery.of(context).size.width,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            mainAxisSize: MainAxisSize.max,
            children: [
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    SizedBox(
                      height: 60,
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          SpinKitThreeBounce(
                            color: Theme.of(context).primaryColor,
                            size: 14.0,
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return _build(context);
  }
}
